/**
* <p>Title: WindowSwitchUtil.java</p>
* <p>Description: 窗口切换utils</p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2015年1月7日
* @version 1.0
*/
package com.hxbd.test.utils.webdriver;

import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.WebDriver;

/**
 * 窗口切换、跳转等相关操作的公共类
 */
public class WindowSwitchUtil {
	private static String Iframe = "Iframe";
	/**
	 * 跳转到打开的新窗口
	 * @param driver
	 * @return WebDriver
	 */
	public static WebDriver SwitchToNewWindows(WebDriver driver){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String currentWindow = driver.getWindowHandle();//获取当前窗口句柄
		Set<String> handles = driver.getWindowHandles();//获取所有窗口句柄
		Iterator<String> it = handles.iterator();
		WebDriver window = null;
		while (it.hasNext()) {
			if (currentWindow == it.next()) {
			continue;
			}
			window = driver.switchTo().window(it.next());
		}
		return window;
	}
//	public static WebDriver SwitchToNewWindows(WebDriver driver, String wintitle){
//		try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String currentWindow = driver.getWindowHandle();//获取当前窗口句柄
//		Set<String> handles = driver.getWindowHandles();//获取所有窗口句柄
//		Iterator<String> it = handles.iterator();
//		while (it.hasNext()) {
//			if (currentWindow == it.next()) {
//			continue;
//			}
//			driver.switchTo().window(it.next());//切换到新窗口
//			String title = driver.getTitle();
//			if (title.contains(wintitle)) {
//				break;
//			}
//			else {
//				continue;
//			}
//		}
//		return driver;
//	}
}
